//
//  ViewController.m
//  FFMusic
//
//  Created by 田侠飞 on 16/7/19.
//  Copyright © 2016年 田侠飞. All rights reserved.
//

#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *bgView;

@property (weak, nonatomic) IBOutlet UILabel *CurrentLabel;

@property (weak, nonatomic) IBOutlet UISlider *ProgressSlider;
@property (weak, nonatomic) IBOutlet UILabel *DurationLabel;

@property (weak, nonatomic) IBOutlet UIButton *PlayButton;

@property (weak, nonatomic) IBOutlet UIButton *NextButton;

@property (weak, nonatomic) IBOutlet UIButton *PreButton;

@property (weak, nonatomic) IBOutlet UILabel *SingerLabel;

@property (weak, nonatomic) IBOutlet UILabel *LyricLabel;

@property (weak, nonatomic) IBOutlet UILabel *AlbumLabel;

@property (weak, nonatomic) IBOutlet UIImageView *AlbumImgV;
@property (weak, nonatomic) IBOutlet UIImageView *HAlbumImgV;
@property (weak, nonatomic) IBOutlet UILabel *HLyricLabel;

//设置定时器
@property (nonatomic, strong) NSTimer *updateTimer;

//模型数组

@property(nonatomic,strong)NSArray * musicList;
//索引
@property(nonatomic,assign)NSInteger currentIndex;
@end

@implementation ViewController

//播放音乐
- (IBAction)play:(id)sender {
    
    TXFMusicModel * music = self.musicList[self.currentIndex];
    
    if (self.PlayButton.selected ) {
        [[TXFMusicManager shareManager] pauseMusic];
        self.PlayButton.selected = NO;
        
        [self.updateTimer invalidate];
    }else{
    
        [[TXFMusicManager shareManager]  playMusicWithFilename:music.mp3];
        
        self.PlayButton.selected = YES;
        
        self.updateTimer = [NSTimer timerWithTimeInterval:0.1 target:self selector:@selector(updateData) userInfo:nil repeats:YES];
        
        [[NSRunLoop currentRunLoop]addTimer:self.updateTimer forMode:NSRunLoopCommonModes];
    
    }
    
    
}

-(void)updateData{

    self.CurrentLabel.text = [self timeStrWithTimeinterval:[TXFMusicManager shareManager].currentTime];
    
    self.AlbumImgV.transform =CGAffineTransformRotate(self.AlbumImgV.transform, M_PI_2*0.02);
    
    self.ProgressSlider.value = [TXFMusicManager shareManager].currentTime/ [TXFMusicManager shareManager].duration;
    
    if ([self.CurrentLabel.text isEqualToString:self.DurationLabel.text]) {
        
        [self next:self.NextButton];
        
    }




}

//转换时间间隔为字符串

-(NSString*)timeStrWithTimeinterval:(NSTimeInterval)timeinterval{


    int  minute = timeinterval/60;
    
    int second = (int)timeinterval % 60;
    
    return [NSString stringWithFormat:@"%02d:%02d",minute,second];



}
//下一曲
- (IBAction)next:(id)sender {
    
    
    
    if (self.currentIndex== self.musicList.count -1) {
        
        self.currentIndex = 0;
        
    }else{
    
        self.currentIndex ++;
    
    }
    
    self.PlayButton.selected = NO;
    
    [self.updateTimer invalidate];
    
    [self prepareData];
}
//上一曲
- (IBAction)pre:(id)sender {
    
    
    if (self.currentIndex== 0) {
        
        self.currentIndex = self.musicList.count -1;
        
    }else{
        
        self.currentIndex --;
        
    }
    
    self.PlayButton.selected = NO;
    [self.updateTimer invalidate];
    [self prepareData];
}

- (IBAction)progressSlider:(UISlider *)sender {
    
    
    [TXFMusicManager shareManager].currentTime = sender.value*[TXFMusicManager shareManager].duration;
    
    
    
}

-(void)viewDidAppear:(BOOL)animated{
    
    [super viewDidAppear:animated];
    
    
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    
    [self configNowPlayingInfoCenter];
    
}
-(void)configNowPlayingInfoCenter{
    
    if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
        
        NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
        [dict setObject:@"name" forKey:MPMediaItemPropertyTitle];
        [dict setObject:@"singer" forKey:MPMediaItemPropertyArtist];
        [dict setObject:@"album" forKey:MPMediaItemPropertyAlbumTitle];
        TXFMusicModel * musicModel = self.musicList[self.currentIndex];
        self.currentIndex++;
       
        UIImage *image = [UIImage imageNamed:musicModel.image];
        
        
        MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
        [dict setObject:artwork forKey:MPMediaItemPropertyArtwork];
        
        [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
        
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置数据:
    
   
    //设置封面视图
    [self  prepareBgView];
    [self prepareAlbumImgV];
     [self prepareData];
    
    
    
}
//设置背景视图
- (void)prepareBgView{

    
    UIBlurEffect * blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    
    UIVisualEffectView * effectView = [[UIVisualEffectView alloc]initWithEffect:blur];
    
    [self.bgView addSubview:effectView];
    
    [effectView mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.edges.mas_equalTo(UIEdgeInsetsZero);
        
    }];
    
    



}
#pragma 设置数据
- (void)prepareData{

//创建模型:
    TXFMusicModel * musicModel = self.musicList[self.currentIndex];
    
    self.bgView.image = [UIImage imageNamed:musicModel.image];
    
    self.AlbumLabel.text = musicModel.album;
    self.AlbumImgV.image = [UIImage imageNamed:musicModel.image];
    self.SingerLabel.text = musicModel.singer;
    
    self.HAlbumImgV.image = [UIImage imageNamed:musicModel.image];
    self.DurationLabel.text = [self timeStrWithTimeinterval:[TXFMusicManager shareManager].duration];
    [self play:self.PlayButton];
    
    
    

}
#pragma 设置封面视图

-(void)prepareAlbumImgV{

    self.AlbumImgV.layer.cornerRadius = 100;
    self.AlbumImgV.layer.masksToBounds = YES;


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//蓝记载

-(NSArray *)musicList{

    if (_musicList ==nil) {
        
        NSString * filePath = [[NSBundle mainBundle]pathForResource:@"mlist.plist" ofType:nil];
        
        NSArray * MusicArr = [NSArray arrayWithContentsOfFile:filePath];
        
        NSMutableArray * arrM = [NSMutableArray array];
        
        for (NSDictionary *dict in MusicArr) {
            
            
            
            TXFMusicModel * model = [TXFMusicModel musicDataDict:dict];
            
            [arrM addObject:model];
            
        }
        
        _musicList = arrM;
        
    }

    return _musicList;
}



@end
